package team.seekers.skill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import team.seekers.skill.entity.query.SkillQuery;
import team.seekers.skill.entity.vo.admin.add.AdminSkillAddVO;
import team.seekers.skill.entity.vo.admin.update.AdminSkillUpdateVO;
import team.seekers.skill.entity.vo.fore.detail.SkillDetailVO;
import team.seekers.skill.result.Result;
import team.seekers.skill.service.SkillService;
import team.seekers.skill.common.utils.ParamCheckUtils;
import team.seekers.skill.common.utils.SkillUtils;
import java.util.Map;

import static team.seekers.skill.result.ResultCode.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbc
 * @since 2021-01-18
 */
@RestController
@RequestMapping("/api/skill/skill")
@Validated
public class SkillController {

    @Autowired
    private SkillService skillService;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result add(@RequestBody @Validated AdminSkillAddVO adminSkillAddVO){
        boolean save = skillService.add(adminSkillAddVO);
        if(!save){
            return Result.fail(ACTION_FAIL);
        }
        return Result.success();
    }

    @DeleteMapping("/delete/{skillId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result delete(@PathVariable("skillId")String skillId){
        if (ParamCheckUtils.isNotRightId(skillId)){
            return Result.fail(PARAM_NOT_VALID);
        }
        boolean remove = skillService.delete(skillId);
        if (!remove){
            return Result.fail(ACTION_FAIL);
        }
        return  Result.success();
    }

    // 只能更新基本信息
    @PutMapping("/update")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result update(@RequestBody @Validated AdminSkillUpdateVO adminSkillUpdateVO){
        if (ParamCheckUtils.isNotRightId(adminSkillUpdateVO.getId())){
            return Result.fail(PARAM_NOT_VALID);
        }
        boolean update = skillService.update(adminSkillUpdateVO);
        if (!update){
            return  Result.fail();
        }
        return Result.success();
    }

    @GetMapping("/getSkillVO/{skillId}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result getSkillVO(@PathVariable("skillId")String skillId){
        if (ParamCheckUtils.isNotRightId(skillId)){
            return Result.fail(MAIN_GET_FAIL);
        }
        SkillDetailVO skillVO = skillService.getSkillDetailVO(skillId);
        if (skillVO == null){
            return Result.fail(GET_FAIL);
        }
        return Result.success().data("skillVo",skillVO);
    }

    @GetMapping("/getAdminSkillVO/{skillId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result getAdminSkillVO(@PathVariable("skillId") String skillId){
        if (ParamCheckUtils.isNotRightId(skillId)){
            return Result.fail(PARAM_NOT_VALID);
        }
        AdminSkillUpdateVO adminSkillUpdateVO = skillService.getAdminSkillVO(skillId);
        if (adminSkillUpdateVO == null){
            return Result.fail(GET_FAIL);
        }
        return Result.success().data("skillDo", adminSkillUpdateVO);
    }

    @PostMapping("/pageSkillCondition/{current}/{limit}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result pageSkillCondition(@PathVariable("current") long current,
                                     @PathVariable("limit") long limit,
                                     @RequestBody SkillQuery skillQuery){
        if(current<1 || limit<1){
            return Result.fail(MAIN_GET_FAIL);
        }
        Map<String, Object> map = skillService.pageSkillCondition(current, limit, skillQuery);
        if(map == null){
            return Result.fail(GET_FAIL);
        }
        return Result.success(map);
    }

    //管理的分页查询
    @PostMapping("/adminPageSkillCondition/{current}/{limit}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result AdminPageSkillCondition(@PathVariable("current") int current,
                                          @PathVariable("limit") int limit,
                                          @RequestBody SkillQuery skillQuery){
        if(current<1 || limit<1){
            return Result.fail(MAIN_GET_FAIL);
        }
        Map<String, Object> map = skillService.adminPageSkillCondition(current, limit, skillQuery);
        if(map == null){
            return Result.fail(GET_FAIL);
        }
        return Result.success(map);
    }

    //开始本技能的学习
    @GetMapping("/startLearn/{skillId}")
    @PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    public Result StartLearn(@PathVariable("skillId")String skillId){
        if(ParamCheckUtils.isNotRightId(skillId)){
            return Result.fail(PARAM_NOT_VALID);
        }
        boolean learn = skillService.startLearn(skillId, SkillUtils.getUserId());
        if(!learn){
            return Result.fail(ACTION_FAIL);
        }
        return Result.success();
    }




}

